package com.xoa.service.attend;

import com.xoa.dao.attend.AttendHolidayMapper;
import com.xoa.dao.attendance.AttendScheduleMapper;
import com.xoa.dao.attendance.UserDutyMapper;
import com.xoa.model.attend.AttendHoliday;
import com.xoa.model.attendance.UserDuty;
import com.xoa.util.ToJson;
import com.xoa.util.attend.AttendDateUtil;
import com.xoa.util.common.L;
import com.xoa.util.common.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.*;

/**
 * 创建作者:   牛江丽
 */
@Service
public class AttendHolidayService {

    @Resource
    private AttendScheduleMapper attendScheduleMapper;

    @Resource
    private AttendHolidayMapper attendHolidayMapper;

    @Resource
    private UserDutyMapper userDutyMapper;

    /**
     * 创建作者:   牛江丽
     * 创建日期:   2018年04月12日 下午10:15:00
     * 方法介绍:   添加节假日
     * 参数说明:   @param autoId
     * 返回值说明:
     */
    @Transactional
    public ToJson<AttendHoliday> insertHoliday(AttendHoliday attendHoliday) {
        ToJson<AttendHoliday> json = new ToJson<AttendHoliday>(1, "error");
        try {
            int count = attendHolidayMapper.insertSelective(attendHoliday);
            if (count > 0) {
//                节假日添加成功后排班表增加排班
                List<UserDuty> values = new ArrayList<>();
                List<Integer> uidList = getUidList(attendHoliday);
                for (Integer uid : uidList) {
                    List<String> days = AttendDateUtil.getDays(AttendDateUtil.getNextDate(attendHoliday.getBeginDate()), attendHoliday.getEndDate());
                    for (String str : days) {
                        UserDuty userDuty = new UserDuty();
                        userDuty.setUid(uid);
                        userDuty.setDutyType(0);
                        userDuty.setDutyDate(str);
                        userDuty.setAddType("3");
                        values.add(userDuty);
                    }
                }
                if(values.size() > 0){
                    userDutyMapper.insertUserDutyByValues(values, 3);
                }
                json.setMsg("ok");
                json.setFlag(0);
            }
        } catch (Exception e) {
            json.setMsg(e.getMessage());
            L.e("AttendHolidayService insertHoliday:" + e);
            e.printStackTrace();
        }
        return json;
    }

    /**
     * 创建作者:   牛江丽
     * 创建日期:   2018年04月12日 下午10:15:00
     * 方法介绍:   修改节假日
     * 参数说明:   @param autoId
     * 返回值说明:
     */
    @Transactional
    public ToJson<AttendHoliday> updateHoliday(AttendHoliday attendHoliday) {
        ToJson<AttendHoliday> json = new ToJson<AttendHoliday>(1, "error");
        try {
            AttendHoliday attendHoliday1 = attendHolidayMapper.selectByPrimaryKey(attendHoliday.getHolidayId());
            if (attendHoliday1 != null) {
//                    删除数据
                Map map = new HashMap();
                List<Integer> listUid = getUidList(attendHoliday1);
                if (listUid.size()>0){
                    map.put("listUid", listUid);

                }
                map.put("dateStart", AttendDateUtil.getNextDate(attendHoliday1.getBeginDate()));
                map.put("dateEnd", attendHoliday1.getEndDate());
                map.put("addType", "3");
                userDutyMapper.deleteUserDutyByUidListAndDate(map);
                userDutyMapper.updateByAddType(map);
            }
            int count = attendHolidayMapper.updateByPrimaryKeySelective(attendHoliday);
            if (count > 0) {
//                    新增数据
                List<UserDuty> values = new ArrayList<>();
                List<Integer> uidList = getUidList(attendHoliday);
                for (Integer uid : uidList) {
                    List<String> days = AttendDateUtil.getDays(AttendDateUtil.getNextDate(attendHoliday.getBeginDate()), attendHoliday.getEndDate());
                    for (String str : days) {
                        UserDuty userDuty = new UserDuty();
                        userDuty.setUid(uid);
                        userDuty.setDutyType(0);
                        userDuty.setDutyDate(str);
                        userDuty.setAddType("3");
                        values.add(userDuty);
                    }
                }
                userDutyMapper.insertUserDutyByValues(values, 3);
                json.setMsg("ok");
                json.setFlag(0);
            }
        } catch (Exception e) {
            json.setMsg(e.getMessage());
            L.e("AttendHolidayService updateHoliday:" + e);
            e.printStackTrace();
        }
        return json;
    }

    /**
     * 创建作者:   牛江丽
     * 创建日期:   2018年04月12日 下午10:15:00
     * 方法介绍:   删除节假日
     * 参数说明:   @param autoId
     * 返回值说明:
     */
    @Transactional
    public ToJson<AttendHoliday> deleteHoliday(int holidayId) {
        ToJson<AttendHoliday> json = new ToJson<AttendHoliday>(1, "error");
        try {
            AttendHoliday attendHoliday = attendHolidayMapper.selectByPrimaryKey(holidayId);
            int count = attendHolidayMapper.deleteByPrimaryKey(holidayId);
            if (attendHoliday != null && count > 0) {
//                    删除数据
                Map map = new HashMap();
                List<Integer> listUid = getUidList(attendHoliday);
                map.put("listUid", listUid);
                map.put("dateStart", AttendDateUtil.getNextDate(attendHoliday.getBeginDate()));
                map.put("dateEnd", attendHoliday.getEndDate());
                map.put("addType", "3");
                userDutyMapper.deleteUserDutyByUidListAndDate(map);
                userDutyMapper.updateByAddType(map);
                json.setMsg("ok");
                json.setFlag(0);
            }
        } catch (Exception e) {
            json.setMsg(e.getMessage());
            L.e("AttendHolidayService deleteHoliday:" + e);
            e.printStackTrace();
        }
        return json;
    }

    /**
     * 创建作者:   牛江丽
     * 创建日期:   2018年04月12日 下午10:15:00
     * 方法介绍:   查询节假日详情
     * 参数说明:   @param autoId
     * 返回值说明:
     */
    public ToJson<AttendHoliday> selHolidayById(int holidayId) {
        ToJson<AttendHoliday> json = new ToJson<AttendHoliday>(1, "error");
        try {
            AttendHoliday attendHoliday = attendHolidayMapper.selectByPrimaryKey(holidayId);
            getdeptNameAndPrivNameAndUserName(attendHoliday);
            json.setObject(attendHoliday);
            json.setMsg("ok");
            json.setFlag(0);
        } catch (Exception e) {
            json.setMsg(e.getMessage());
            L.e("AttendHolidayService selHolidayById:" + e);
            e.printStackTrace();
        }
        return json;
    }

    /**
     * 创建作者:   牛江丽
     * 创建日期:   2018年04月12日 下午10:15:00
     * 方法介绍:   查询节假日
     * 参数说明:   @param autoId
     * 返回值说明:
     */
    public ToJson<AttendHoliday> selHoliday(AttendHoliday attendHoliday) {
        ToJson<AttendHoliday> json = new ToJson<AttendHoliday>(1, "error");
        try {
            List<AttendHoliday> list = attendHolidayMapper.selectHoliday(attendHoliday);
            for (AttendHoliday attendHoliday1 : list) {
                getdeptNameAndPrivNameAndUserName(attendHoliday1);
            }
            json.setObj(list);
            json.setMsg("ok");
            json.setFlag(0);
        } catch (Exception e) {
            json.setMsg(e.getMessage());
            L.e("AttendHolidayService selHoliday:" + e);
            e.printStackTrace();
        }
        return json;
    }

    //获得当前节假日的的所有uid
    public List<Integer> getUidList(AttendHoliday attendHoliday) {
        Set<Integer> uidList = new HashSet<>();
        //获取当前的排班人员
        //部门下
        Map map = new HashMap();
        if (!StringUtils.checkNull(attendHoliday.getDeptId())) {
            if (!attendHoliday.getDeptId().equals("ALL_DEPT")) {
                map.put("deptIds", attendHoliday.getDeptId().split(","));
                List<Integer> uids = attendScheduleMapper.getUidListByDeptIds(map);
                uidList.addAll(uids);
            } else {
                List<Integer> uids = attendScheduleMapper.getUidAll(map);
                uidList.addAll(uids);
            }
        }
        //角色下
        if (!StringUtils.checkNull(attendHoliday.getPrivId())) {
            map.put("privIds", attendHoliday.getPrivId().split(","));
            List<Integer> uids = attendScheduleMapper.getUidListByPrivIds(map);
            uidList.addAll(uids);
        }
        //userId
        if (!StringUtils.checkNull(attendHoliday.getUserId())) {
            String[] userids = attendHoliday.getUserId().split(",");
            List<Integer> uids = attendScheduleMapper.getUidListByUserIds(userids);
            uidList.addAll(uids);
        }
        return new ArrayList<>(uidList);
    }


    /**
     * 获取id对应的名字
     */
    public void getdeptNameAndPrivNameAndUserName(AttendHoliday attendHoliday) {
        if (attendHoliday != null) {
            //获取部门的name
            if (attendHoliday.getDeptId() != null && attendHoliday.getDeptId().length() > 0) {
                if (!attendHoliday.getDeptId().equals("ALL_DEPT")) {
                    List<String> deptNames = attendScheduleMapper.getDeptNames(attendHoliday.getDeptId());
                    if (deptNames != null && deptNames.size() > 0) {
                        StringBuffer deptName = new StringBuffer();
                        for (int i = 0; i < deptNames.size(); i++) {
                            deptName.append(deptNames.get(i) + ",");
                        }
                        attendHoliday.setDeptName(deptName.toString());
                    }
                } else {
                    attendHoliday.setDeptName("全部部门");
                }
            }

            //获取角色的name
            if (attendHoliday.getPrivId() != null && attendHoliday.getPrivId().length() > 0) {
                List<String> privNames = attendScheduleMapper.getprivNames(attendHoliday.getPrivId());
                if (privNames != null && privNames.size() > 0) {
                    StringBuffer privName = new StringBuffer();
                    for (int i = 0; i < privNames.size(); i++) {
                        privName.append(privNames.get(i) + ",");
                    }
                    attendHoliday.setPrivName(privName.toString());
                }
            }
            //获取用户的name
            if (attendHoliday.getUserId() != null && attendHoliday.getUserId().length() > 0) {
                List<String> userNames = attendScheduleMapper.getuserNames(attendHoliday.getUserId());
                if (userNames != null && userNames.size() > 0) {
                    StringBuffer userName = new StringBuffer();
                    for (int i = 0; i < userNames.size(); i++) {
                        userName.append(userNames.get(i) + ",");
                    }
                    attendHoliday.setUserName(userName.toString());
                }
            }
        }
    }

}
